﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Animations;
using System;

namespace AnimPlayable
{
    // A behaviour that is attached to a playable
    public class AnimPlaybaleBehavior : PlayableBehaviour
    {
        public AnimWidget animWidget;

        // Called when the owning graph starts playing
        public override void OnGraphStart(Playable playable)
        {
            Debug.LogError("OnGraphStart   " + playable.ToString());
        }

        // Called when the owning graph stops playing
        public override void OnGraphStop(Playable playable)
        {
            Debug.LogError("OnGraphStop   ");
        }

        // Called when the state of the playable is set to Play
        public override void OnBehaviourPlay(Playable playable, FrameData info)
        {
            Debug.LogError("OnBehaviourPlay   ");
        }

        // Called when the state of the playable is set to Paused
        public override void OnBehaviourPause(Playable playable, FrameData info)
        {
            Debug.LogError("OnBehaviourPause   ");
        }

        float fadeWeight = 0;
        public override void PrepareFrame(Playable playable, FrameData info)
        {
            if (animWidget.currAnimState == null) return;
            float deltaTime = info.deltaTime;
            animWidget.currAnimState.useTime += deltaTime;
            //Playable = this = AnimPlaybaleBehavior
            Playable mixer = playable.GetInput(0);
            if (mixer.GetInputCount() == 2)
            {
                Playable clip1 = mixer.GetInput(0);
                Playable clip2 = mixer.GetInput(1);
                fadeWeight += deltaTime * 3f;
                if (fadeWeight > 1)
                {
                    //混合完成
                    fadeWeight = 0;
                    mixer.DisconnectInput(1);
                    mixer.DisconnectInput(0);
                    mixer.SetInputCount(1);
                    mixer.ConnectInput(0, clip2, 0);
                    mixer.SetInputWeight(0, 1);
                }
                else
                {
                    mixer.SetInputWeight(0, 1 - fadeWeight);
                    mixer.SetInputWeight(1, fadeWeight);
                }
            }
            else
            {
                if (animWidget.currAnimState.mode == Anim_Mode.Once)
                {
                    Playable clip1 = mixer.GetInput(0);
                    float len = animWidget.currAnimState.animLength;
                    bool timeEnd = animWidget.currAnimState.useTime >= len;
                    if (timeEnd)
                    {
                        if (animWidget.QueueCount > 0)
                        {
                            this.animWidget.onAnimEnd();
                        }
                        else
                        {
                            this.animWidget.playAnim("role_03_idle", Anim_Mode.Loop);
                        }
                    }
                }
                else if (animWidget.currAnimState.mode == Anim_Mode.Loop)
                {
                    Playable clip1 = mixer.GetInput(0);
                    float len = animWidget.currAnimState.animLength;
                    bool timeEnd = animWidget.currAnimState.useTime >= len;
                    if (timeEnd)
                    {
                        animWidget.currAnimState.useTime = 0;
                        Debug.Log("end");                       
                    }
                }
            }
        }

    }

}
